package com.lijinjiang.proxy03;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName ProxyUtil
 * @Description TODO
 * @Author Li
 * @Date 2022/9/21 17:56
 * @ModifyDate 2022/9/21 17:56
 * @Version 1.0
 */
public class ProxyUtil {
    public static <T> T getProxy(T obj) {
        return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader()
                , obj.getClass().getInterfaces()
                , new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        long startTime = System.currentTimeMillis();
                        Object res = method.invoke(obj, args);
                        long endTime = System.currentTimeMillis();
                        System.out.println("login方法耗时：" + (endTime - startTime) / 1000.0 + "s");
                        return res;
                    }
                });
    }
}
